<?php
/**
 * Created by xun at 2021/4/26
 */

namespace console\controllers;

use common\models\c2\entity\Collection;
use common\models\c2\entity\Venue;
use common\models\c2\statics\CollectionStatus;
use common\models\c2\statics\EntityDeleteStatus;
use common\models\c2\statics\VenueStatus;
use cza\base\models\statics\EntityModelState;
use yii\console\Controller;
use yii\db\Exception;
use yii\helpers\ArrayHelper;

class VenueController extends Controller
{

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
    }

    public function actionIndex()
    {
        $venues = Venue::find()
            ->with('collections')
            ->where(['is_deleted' => EntityDeleteStatus::STATUS_NOT_DELETED])
            ->all();

        $time = time();
        foreach ($venues as $venue) {

            // print_r("现在时间:" . $time . "\n\r");
            // print_r("开放时间:" . strtotime($venue->open_at) . "\n\r");
            // print_r("关闭时间:" . strtotime($venue->close_at) . "\n\r");

            if ($time >= strtotime($venue->open_at) && $time < strtotime($venue->close_at)) {
                $collections = $venue->collections;
                foreach ($collections as $collection) {
                    /** @var $collection Collection */
                    if ($collection->status == CollectionStatus::STATUS_TO_BE_OPEN) {
                        \Yii::info("开放藏品:" . $collection->id . "\n\r");
                        $collection->updateAttributes(['status' => CollectionStatus::STATUS_DEFAULT]);
                    }
                }
                $venue->updateAttributes(['status' => VenueStatus::STATUS_ACTIVE]);
            } else {
                $collections = $venue->collections;
                foreach ($collections as $collection) {
                    /** @var $collection Collection */
                    $collection->updateAttributes(['status' => CollectionStatus::STATUS_TO_BE_OPEN]);
                }
                $venue->updateAttributes(['status' => VenueStatus::STATUS_INACTIVE]);
            }
        }
    }

}